﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendSMS.aspx.cs" Inherits="CRMWeb.Pages.SendSMS" %>
 
<html>
<head id="Head1" runat="server">
    <title>Send SMS</title>
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="/Css/CrmStyle.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/EasyUI/jquery.easyui.min.js"></script>
</head>
<body>
    <form runat="server">
        <div id="pnlSMS" class="easyui-panel" style="width: auto; height: auto">
            <div class="" data-options="fit:true">
                <table style="width: 100%; height: auto">
                    <tr>
                        <td style="width: 70%; text-align: left">
                            <div class="sms_dropdown">
                                <label style="text-align: left">SMS Tempelate:</label>
                                <select class="easyui-combotree" id="ddlSMSTemplate1" style="width: 520px;"
                                    data-options="url:'/WebServices/SMSTemplate.ashx?method=DisplayTemplateNode', onSelect:selectSMSTemplate">
                                </select>
                            </div>
                            <div class="fitem sms_dropdown">
                                <label style="text-align: left">Mobile No:</label>
                                <select class="easyui-combogrid" id="cmbmobileno" style="width: 304px" runat="server" name="cmbmobileno" data-options="  
                                panelWidth: 304,  
                                multiple: true,
                                idField: 'Mobile',  
                                textField: 'Mobile',
                                editable:true,  
                                url: '/WebServices/SMSHandler.ashx?method=getAllMobileNo&ID=' + getQueryParameter()+ '&isAppID='+ getQuerystring('isAppID',''),
                                columns: [[  
                                {field:'ck',checkbox:true},
                                {field:'FirstName',width:80},      
                                ]], 
                                 onSelect:getSelections, 
                                 onUnselect:getUnselected,
                                fitColumns: true  
                                ">
                                </select>
                                <span id="spnInvalidNumber" style="display: none; width: auto">
                                    <label id="lblAlert" style="color: red; width: auto">Please enter valid Mobile number ! </label>
                                </span>
                                <input id="hdnUserName" runat="server" type="hidden" />
                            </div>
                        </td>
                    </tr>
                </table>
                <div data-options="region:'center'" style="padding: 10px; height: auto" class="smsbox">
                    <form name="myform">
                        <textarea id="txtsmsdescription" name="SMSDescription" style="font-family: Arial" class="easyui-validatebox" required="true" onkeydown="limitText()" onkeyup="limitText();"></textarea>
                        <div id="divLeftCharacter">
                            You have
                    <label id="lblCountDown">159</label>
                            characters left.
                        </div>
                        <div id="divMaxLimit" style="display: none; color: red;">
                            This SMS will be charged  twice as its over 159 characters
                        </div>
                    </form>
                </div>
                <asp:ScriptManager ID="sm1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
                <div data-options="region:'south',border:false" style="text-align: right; padding: 5px 0 0;">
                    <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="margin-right: 15px" href="javascript:void(0)" onclick="return isSMSValid()">Send</a>
                </div>
            </div>
        </div>
    </form>
</body>
<script type="text/javascript">
    function IsValidate() {
        var flag = true;
        //if ($('#ddlSMSTemplate1').combobox('getValue') == '') {
        //    $.messager.alert('Morgan CRM', 'Select SMS Template ', 'info');
        //    return flag = false;
        //}
        if ($('#cmbmobileno').combogrid('getText') == '') {
            $.messager.alert('Morgan CRM', 'Select Mobile Number ', 'info');
            return flag = false;
        }
        return flag;
    }
    var SMSOpr;
    $(document).ready(function () {
        $('#ddlSMSTemplate1').combotree({
            onClick: function (node) {
                if (!$('#ddlSMSTemplate1').tree('isLeaf', node.target)) {
                    $('#ddlSMSTemplate1').combotree('setValues', '');
                    var t = $('#ddlSMSTemplate1').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#ddlSMSTemplate1').combotree('panel');
                    p.panel('open');
                }
            }
        });

        SMSOpr = document.getElementById('hdnUserName').value;
        $('#pnlSMS').panel('resize', {
            width: window.innerWidth,
            height: window.innerHeight
        });
        $('#txtsmsdescription').css('height', window.innerHeight / 2);
        $('#ddlSMSTemplate1').combobox('textbox').bind('click', function () {
            $('#ddlSMSTemplate1').combobox('showPanel');
        });
        $('#cmbmobileno').combobox('textbox').bind('click', function () {
            $('#cmbmobileno').combobox('showPanel');
        });
        $("#cmbmobileno").combogrid('setValue', getQuerystring('mobileno', ''));
    });
    function selectSMSTemplate(node) {
        var currentTemplateId = node.id;
        $.ajax({
            url: "/WebServices/SMSTemplate.ashx?method=GetSMSTemplateContentById",
            contentType: "application/json; charset=utf-8",
            data: { smsTemplateId: currentTemplateId },
            success: function (data) {
                $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {
                    if (getQuerystring('contact_id', '') != '')
                        showContactData(result.TemplateFormat);
                    else if (getQuerystring('app_id', '') != '')
                        showApplicationData(result.TemplateFormat);
                    else if (getQuerystring('contact_id', '') == '' && getQuerystring('app_id', '') == '')
                        $('#txtsmsdescription').val(result.TemplateFormat);
                    setTimeout(function () {
                        limitText();
                    }, 500);
                });
            },
            error: function () {
                alert("Error while getting template.");
            }
        });
    }
    function getQueryParameter() {
        var queryString = window.location.search.substring(1);
        var queryparam = queryString.split("=");
        var firstParameter = queryparam[1].split("&");
        return firstParameter[0];
    }
    var contactno;
    var hdnTextFirstName;
    var fsName;
    function getSelections() {
        var ss = [];
        var r = $('#cmbmobileno').combogrid('grid');
        var rows = r.datagrid('getSelections');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            var temp = (row.Mobile);
            fsName = (row.FirstName);
            if (temp.length == 0)
                return;
            else
                ss.push(row.Mobile);
        }
        contactno = ss.join(';').replace(',', ';');
        contactno = $("#cmbmobileno").combogrid('getText').replace(/,/g, ';');
    }
    function getUnselected() {
        contactno = '';
        hdnTextFirstName = '';
    }
    function limitText() {
        var limitField = $('#txtsmsdescription').val();
        var limitCount = $('#lblCountDown').text();
        var limitNum = 159;

        if (Number(limitField.length) > Number(limitNum)) {
            $('#divMaxLimit').show();
            $('#divLeftCharacter').hide();
        } else {
            $('#divMaxLimit').hide();
            $('#divLeftCharacter').show();

            var temp = Number(limitNum) - Number(limitField.length);
            $('#lblCountDown').html(temp);

            if (temp == 0) {
                $('#divMaxLimit').show();
                $('#divLeftCharacter').hide();
            }
        }
    }
    window.onresize = function () {
        $('#pnlSMS').panel('resize', {
            width: window.innerWidth,
            height: window.innerHeight
        });
        $('#txtsmsdescription').css('height', window.innerHeight / 2);
    }
    function getQuerystring(key, defaultValue) {
        if (defaultValue == null) defaultValue = "";
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if (qs == null)
            return defaultValue;
        else {
            return qs[1];
        }
    }
    function showContactData(SMSTemplateFormat) {
        $.ajax({
            url: "/WebServices/ApplicationContact.ashx?method=ApplicationContactSelect",
            contentType: "application/json; charset=utf-8",
            data: { 'ContactID': getQuerystring('contact_id', '') },
            success: function (data) {
                $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {

                    //ClientFileds
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ContactID (Client)%', rec.ContactID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%FirstName (Client)%', rec.FirstName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%MiddleName (Client)%', rec.MiddleName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SurName (Client)%', rec.SurName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Title (Client)%', rec.Title);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Mobile (Client)%', rec.Mobile);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%HomePhone (Client)%', rec.HomePhone);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Email (Client)%', rec.Email);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DriverLicense (Client)%', rec.DriverLicense);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Citizenship (Client)%', rec.Citizenship);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DateBirth (Client)%', rec.DateBirth);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Street (Client)%', rec.Street);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Suburb (Client)%', rec.Suburb);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%State (Client)%', rec.State);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PostCode (Client)%', rec.PostCode);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ResidencePeriod (Client)%', rec.ResidencePeriod);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ResidenceLength (Client)%', rec.ResidenceLength);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Dependant (Client)%', rec.Dependant);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_ResidentialStatus (Client)%', rec.app_ResidentialStatus);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_number (Client)%', rec.app_number);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_id (Client)%', rec.app_id);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%NumberFromAppinfo (Client)%', rec.NumberFromAppinfo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Memo (Client)%', rec.Memo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%CreateDate (Client)%', rec.CreateDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EditDate (Client)%', rec.EditDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DupwithContact (Client)%', rec.DupwithContact);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%deletedFlag (Client)%', rec.deletedFlag);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Role (Client)%', rec.Role);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Relationship (Client)%', rec.Relationship);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%WorkPhone (Client)%', rec.WorkPhone);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%FaxNumber (Client)%', rec.FaxNumber);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Email1 (Client)%', rec.Email1);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PostalAddress (Client)%', rec.PostalAddress);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%IncomeType (Client)%', rec.IncomeType);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpTypeofEntity (Client)%', rec.SelfEmpTypeofEntity);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpBusinessName (Client)%', rec.SelfEmpBusinessName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpABN (Client)%', rec.SelfEmpABN);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpNatureofBusiness (Client)%', rec.SelfEmpNatureofBusiness);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpPhone (Client)%', rec.SelfEmpPhone);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpAddress (Client)%', rec.SelfEmpAddress);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%KinsName (Client)%', rec.KinsName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%KinsRelationship (Client)%', rec.KinsRelationship);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%KinsContactNumber (Client)%', rec.KinsContactNumber);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Notes (Client)%', rec.Notes);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpEmployer (Client)%', rec.EmpEmployer);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpOccupation (Client)%', rec.EmpOccupation);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpJobPosition (Client)%', rec.EmpJobPosition);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpEmployerPhone (Client)%', rec.EmpEmployerPhone);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpContactPerson (Client)%', rec.EmpContactPerson);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpWorkAddress (Client)%', rec.EmpWorkAddress);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%comesfrom (Client)%', rec.comesfrom);

                    //LoanFileds
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanID (Loan)%', rec.LoanID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanNumber (Loan)%', rec.LoanNumber);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PaymentPeriod (Loan)%', rec.PaymentPeriod);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DrawDownDate (Loan)%', rec.DrawDownDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanStatusID (Loan)%', rec.LoanStatusID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanTerm (Loan)%', rec.LoanTerm);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%FirstPaymentDue (Loan)%', rec.FirstPaymentDue);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LowerRate (Loan)%', rec.LowerRate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%HigherRate (Loan)%', rec.HigherRate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%InterestMethod (Loan)%', rec.InterestMethod);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_id (Loan)%', rec.app_id);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Lender (Loan)%', rec.Lender);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Product (Loan)%', rec.Product);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Type (Loan)%', rec.Type);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Purpose (Loan)%', rec.Purpose);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanAmount (Loan)%', rec.LoanAmount);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DishonorFee (Loan)%', rec.DishonorFee);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Memo (Loan)%', rec.Memo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%NewDDRDate (Loan)%', rec.NewDDRDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%BankAccDate (Loan)%', rec.BankAccDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PaymentSchDate (Loan)%', rec.PaymentSchDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EffectiveDate (Loan)%', rec.EffectiveDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DefaultNotice (Loan)%', rec.DefaultNotice);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DoubleChecked (Loan)%', rec.DoubleChecked);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%CreateDate (Loan)%', rec.CreateDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EditDate (Loan)%', rec.EditDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LastResult (Loan)%', rec.LastResult);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EzidebitRefNo (Loan)%', rec.EzidebitRefNo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%OperatorName%', SMSOpr);
                    $('#txtsmsdescription').val(SMSTemplateFormat);
                });
            }
        });
    }
    function showApplicationData(SMSTemplateFormat) {
        $.ajax({
            url: "/WebServices/ApplicationInfo.ashx?method=BindApplicationForSMSFields",
            contentType: "application/json; charset=utf-8",
            data: { 'app_id': getQuerystring('app_id', '') },
            success: function (data) {
                $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {

                    //ApplicationFileds
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_id (Application)%', rec.app_id);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%deletedFlag (Application)%', rec.deletedFlag);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_result (Application)%', rec.app_result);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ResultID (Application)%', rec.ResultID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoadNo (Application)%', rec.LoadNo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Folder (Application)%', rec.Folder);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%AwaitDocs (Application)%', rec.AwaitDocs);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%NoInApplication (Application)%', rec.NoInApplication);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%OfferedAmount (Application)%', rec.OfferedAmount);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DesiredAmount (Application)%', rec.DesiredAmount);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%amountMoto (Application)%', rec.amountMoto);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%amoutProperties (Application)%', rec.amoutProperties);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%TextAwaitDoc (Application)%', rec.TextAwaitDoc);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PurposeSummary (Application)%', rec.PurposeSummary);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Unsubscribe_Ad (Application)%', rec.Unsubscribe_Ad);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Memo (Application)%', rec.Memo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LenderID (Application)%', rec.LenderID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%OutSrcLenderVal (Application)%', rec.OutSrcLenderVal);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ReferrerID (Application)%', rec.ReferrerID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ReferrerVal (Application)%', rec.ReferrerVal);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%CreatedDate (Application)%', rec.CreatedDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EditDate (Application)%', rec.EditDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%comesfrom (Application)%', rec.comesfrom);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%securities (Application)%', rec.securities);

                    //ClientFileds
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ContactID (Client)%', rec.ContactID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%FirstName (Client)%', rec.FirstName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%MiddleName (Client)%', rec.MiddleName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SurName (Client)%', rec.SurName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Title (Client)%', rec.Title);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Mobile (Client)%', rec.Mobile);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%HomePhone (Client)%', rec.HomePhone);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Email (Client)%', rec.Email);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DriverLicense (Client)%', rec.DriverLicense);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Citizenship (Client)%', rec.Citizenship);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DateBirth (Client)%', rec.DateBirth);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Street (Client)%', rec.Street);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Suburb (Client)%', rec.Suburb);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%State (Client)%', rec.State);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PostCode (Client)%', rec.PostCode);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ResidencePeriod (Client)%', rec.ResidencePeriod);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%ResidenceLength (Client)%', rec.ResidenceLength);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Dependant (Client)%', rec.Dependant);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_ResidentialStatus (Client)%', rec.app_ResidentialStatus);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_number (Client)%', rec.app_number);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_id (Client)%', rec.app_id);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%NumberFromAppinfo (Client)%', rec.NumberFromAppinfo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Memo (Client)%', rec.Memo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%CreateDate (Client)%', rec.CreateDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EditDate (Client)%', rec.EditDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DupwithContact (Client)%', rec.DupwithContact);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%deletedFlag (Client)%', rec.deletedFlag);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Role (Client)%', rec.Role);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Relationship (Client)%', rec.Relationship);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%WorkPhone (Client)%', rec.WorkPhone);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%FaxNumber (Client)%', rec.FaxNumber);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Email1 (Client)%', rec.Email1);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PostalAddress (Client)%', rec.PostalAddress);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%IncomeType (Client)%', rec.IncomeType);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpTypeofEntity (Client)%', rec.SelfEmpTypeofEntity);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpBusinessName (Client)%', rec.SelfEmpBusinessName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpABN (Client)%', rec.SelfEmpABN);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpNatureofBusiness (Client)%', rec.SelfEmpNatureofBusiness);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpPhone (Client)%', rec.SelfEmpPhone);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%SelfEmpAddress (Client)%', rec.SelfEmpAddress);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%KinsName (Client)%', rec.KinsName);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%KinsRelationship (Client)%', rec.KinsRelationship);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%KinsContactNumber (Client)%', rec.KinsContactNumber);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Notes (Client)%', rec.Notes);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpEmployer (Client)%', rec.EmpEmployer);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpOccupation (Client)%', rec.EmpOccupation);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpJobPosition (Client)%', rec.EmpJobPosition);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpEmployerPhone (Client)%', rec.EmpEmployerPhone);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpContactPerson (Client)%', rec.EmpContactPerson);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EmpWorkAddress (Client)%', rec.EmpWorkAddress);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%comesfrom (Client)%', rec.comesfrom);

                    //LoanFileds
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanID (Loan)%', rec.LoanID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanNumber (Loan)%', rec.LoanNumber);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PaymentPeriod (Loan)%', rec.PaymentPeriod);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DrawDownDate (Loan)%', rec.DrawDownDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanStatusID (Loan)%', rec.LoanStatusID);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanTerm (Loan)%', rec.LoanTerm);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%FirstPaymentDue (Loan)%', rec.FirstPaymentDue);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LowerRate (Loan)%', rec.LowerRate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%HigherRate (Loan)%', rec.HigherRate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%InterestMethod (Loan)%', rec.InterestMethod);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%app_id (Loan)%', rec.app_id);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Lender (Loan)%', rec.Lender);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Product (Loan)%', rec.Product);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Type (Loan)%', rec.Type);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Purpose (Loan)%', rec.Purpose);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LoanAmount (Loan)%', rec.LoanAmount);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DishonorFee (Loan)%', rec.DishonorFee);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%Memo (Loan)%', rec.Memo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%NewDDRDate (Loan)%', rec.NewDDRDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%BankAccDate (Loan)%', rec.BankAccDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%PaymentSchDate (Loan)%', rec.PaymentSchDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EffectiveDate (Loan)%', rec.EffectiveDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DefaultNotice (Loan)%', rec.DefaultNotice);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%DoubleChecked (Loan)%', rec.DoubleChecked);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%CreateDate (Loan)%', rec.CreateDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EditDate (Loan)%', rec.EditDate);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%LastResult (Loan)%', rec.LastResult);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%EzidebitRefNo (Loan)%', rec.EzidebitRefNo);
                    SMSTemplateFormat = SMSTemplateFormat.replace('%OperatorName%', SMSOpr);
                    $('#txtsmsdescription').val(SMSTemplateFormat);
                });
            }
        });
    }
    function onSuccess() {
        $('#cmbmobileno').combobox('setText', '');
        $('#txtsmsdescription').val('');
        window.opener.location.reload();
        window.close();
    }
    function sendSMS(mobileNumbers) {
        $.ajax({
            url: '/WebServices/SMSHandler.ashx',
            type: 'POST',
            data: {
                method: 'SendSMS',
                args: { MobileNo: mobileNumbers, Message: $('#txtsmsdescription').val() }
            },
            success: function (data) {
                //insert record in history table
                var app_id = getQuerystring('app_id', 0);
                var ContactID = getQuerystring('contact_id', 0);
                var dt = new Date();
                var HistoryDate = (dt.getMonth() + 1) + '/' + dt.getDate() + '/' + dt.getFullYear();
                var HistoryTime = dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds();
                var Action = 'SMS Sent(system)';
                var Operator = $('#hdnUserName').val();
                var Description = 'To: ' + fsName + '</br>' + 'Mobile No. : ' + $('#cmbmobileno').combobox('getText') + '<br/></br>SMS Content : ' + $('#txtsmsdescription').val();
                Description = encodeURIComponent(Description);

                if (app_id > 0) {
                    PageMethods.saveNewHistory('Application', '0', HistoryDate, HistoryTime, Action, Operator, Description, app_id);
                    setTimeout(function () {
                        onSuccess();
                    }, 100);
                }
                else if (ContactID > 0) {
                    PageMethods.saveNewHistory('Contact', ContactID, HistoryDate, HistoryTime, Action, Operator, Description, '0');
                    setTimeout(function () {
                        onSuccess();
                    }, 100);
                }
                else
                    onSuccess();

                /*if (app_id != '' || ContactID != '') {
                    if (app_id == '')
                        app_id = 0;
                    else
                        ContactID = 0;
                    $.ajax({
                        url: '/WebServices/SMSHandler.ashx',
                        type: 'POST',
                        data: {
                            method: 'AddHistory',
                            args: { 'app_id': app_id, 'ContactID': ContactID, 'HistoryDate': HistoryDate, 'HistoryTime': HistoryTime, 'Action': Action, 'Operator': Operator, 'Description': Description }
                        },
                        success: function (data) {
                            onSuccess();
                        }
                    });
                }
                else
                    onSuccess();
               */
            }
        });
    }
  
    function isSMSValid() {
        if (IsValidate()) {
            if ($('#cmbmobileno').combobox('getText') == '' || $('#txtsmsdescription').val() == '') {
                return false;
            }
            var nos = $('#cmbmobileno').combobox('getText');
            nos = nos.replace(/,,*/g, ',');
            nos = nos.replace(/^,/, '');
            nos = nos.replace(/$,/, '');
            $.ajax({
                url: '/WebServices/SMSHandler.ashx?method=CheckMobileNos',
                data: { 'MobileNo': nos },
                success: function (response) {
                    if (response == 1)
                        sendSMS(nos);
                    else
                        $('#spnInvalidNumber').show();
                }
            });
        }
    }
</script>
</html>
